package jemuillot.BiohazardPanelPuzzle;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.handler.timer.ITimerCallback;
import org.anddev.andengine.engine.handler.timer.TimerHandler;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.util.HorizontalAlign;

public class MainMenuScene extends BiohazardPanelPuzzleScene {

	static final int LAYER_TITLE = LAYER_BACKGROUND + 1;
	static final int LAYER_SELECTIONS = LAYER_TITLE + 1;

	static final int LAYERS = LAYER_SELECTIONS + 1;

	private float titleCenterX;
	private float titleCenterY;

	private Sprite title;
	private float titleY;

	private Text selStart;
	private Text selOptions;
	private Text selExit;

	private boolean titleMoving;

	private float selAlpha;
	private float alphaStep;
	private TimerHandler timer;

	public MainMenuScene(BiohazardPanelPuzzle bpp) {
		super(bpp, LAYERS);

		scene.setBackgroundEnabled(false);

		scene.getLayer(LAYER_BACKGROUND).addEntity(
				new Sprite(0, 0, bpp.backgroundRegion));

		titleCenterX = getCenterX(bpp.titleRegion);
		titleCenterY = getCenterY(bpp.titleRegion);

		titleY = CAMERA_HEIGHT + 90;

		title = new Sprite(titleCenterX, titleY, bpp.titleRegion);

		scene.getLayer(LAYER_TITLE).addEntity(title);

		titleMoving = true;

		float selectionY = SELECTION_OFFSET + titleCenterY;
		
		String thisText = getString(R.string.start);

		selStart = new Text(
				(CAMERA_WIDTH - bpp.font.getStringWidth(thisText)) / 2,
				selectionY, bpp.font, thisText, HorizontalAlign.CENTER) {
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
					final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN)
					gameStart();
				return true;
			}
		};

		selStart.setBlendFunction(GL10.GL_SRC_ALPHA,
				GL10.GL_ONE_MINUS_SRC_ALPHA);

		scene.getLayer(LAYER_SELECTIONS).addEntity(selStart);
		
		thisText = getString(R.string.options);

		selOptions = new Text((CAMERA_WIDTH - bpp.font
				.getStringWidth(thisText)) / 2, selectionY + SELECTION_DIST,
				bpp.font, thisText, HorizontalAlign.CENTER){
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
					final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN)
					options();
				return true;
			}
		};

		selOptions.setBlendFunction(GL10.GL_SRC_ALPHA,
				GL10.GL_ONE_MINUS_SRC_ALPHA);

		thisText = getString(R.string.exit);
		scene.getLayer(LAYER_SELECTIONS).addEntity(selOptions);

		selExit = new Text(
				(CAMERA_WIDTH - bpp.font.getStringWidth(thisText)) / 2,
				selectionY + SELECTION_DIST * 2, bpp.font, thisText,
				HorizontalAlign.CENTER) {
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
					final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN)
					finish();
				return true;
			}
		};

		selExit
				.setBlendFunction(GL10.GL_SRC_ALPHA,
						GL10.GL_ONE_MINUS_SRC_ALPHA);

		scene.getLayer(LAYER_SELECTIONS).addEntity(selExit);

		selAlpha = 0;
		alphaStep = 0;

		selStart.setAlpha(0);
		selOptions.setAlpha(0);
		selExit.setAlpha(0);

		scene.registerUpdateHandler(timer = new TimerHandler(0.05f, true,
				new ITimerCallback() {

					@Override
					public void onTimePassed(final TimerHandler pTimerHandler) {
						if (titleMoving) {
							titleY -= 10;
							title.setPosition(titleCenterX, titleY);

							selAlpha += alphaStep;

							if (selAlpha > 1.0f)
								selAlpha = 1.0f;

							selStart.setAlpha(selAlpha);
							selOptions.setAlpha(selAlpha);
							selExit.setAlpha(selAlpha);

							if (titleY < 90) {
								titleMoving = false;

								scene.registerTouchArea(selStart);
								scene.registerTouchArea(selOptions);
								scene.registerTouchArea(selExit);

								scene.setTouchAreaBindingEnabled(true);

							}

							if (titleY < 200) {
								alphaStep = 0.08f;
							}
						} else {
							scene.unregisterUpdateHandler(timer);
						}

					}
				}));
	}

	protected void options() {
		new OptionsScene(bpp);
	}

	protected void finish() {
		bpp.finish();
	}

	protected void gameStart() {
		new LevelSelectScene(bpp);
	}
}
